<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>主存</title>
    <!-- 引入组件库 -->
    <script src="../js/vue.js"></script>
    <link rel="stylesheet" href="../element-ui/lib/theme-chalk/index.css">
    <script src="../element-ui/lib/index.js"></script>
    <script src="../js/axios.js"></script>
    <style>
        .el-header {
            /*background-color: #409EFF;*/
            /*color: #FFFFFF;*/
            line-height: 60px;
        }

        .el-main {
            line-height: 400px;
        }

        .el-aside {
            color: #333;
        }

        body > .el-container {
            margin-bottom: 40px;
        }

        body {
            background-image: url("https://cloud-minapp-42632.cloud.ifanrusercontent.com/1p6Nb7qMoRvUNbr3.jpg");
            background-repeat: no-repeat;
            background-size: auto 100%;
            background-attachment: fixed;
        }

        .grid-content1, .grid-content2 {
            margin-top: 60px;
            height: 350px;
            display: flex;
            justify-content: center;
            align-items: center;
        }

        .el-form {
            padding: 60px 40px 40px 20px;
            background-color: white;
            border-radius: 20px;

        }

        .el-button {
            width: 100%
        }
    </style>
</head>
<body>
<div id="app">
    <el-container>
        <el-header></el-header>
        <el-main>
            <el-row>
                <el-col :span="14">
                    <div class="grid-content1">
                    </div>
                </el-col>
                <el-col :span="10">
                    <div class="grid-content2">
                        <el-form :model="formLabelAlign" label-position="right" :rules="rules" ref="formLabelAlign"
                                 label-width="80px">
                            <el-form-item label="手机号" prop="phone">
                                <el-input type="number" placeholder="请输入11位手机号"
                                          v-model="formLabelAlign.phone"></el-input>
                            </el-form-item>
                            <el-form-item label="密码" prop="password">
                                <el-input placeholder="请输入密码" type="password"
                                          v-model="formLabelAlign.password"></el-input>
                            </el-form-item>
                            <el-form-item>
                                <el-button type="primary" :loading="load" @click="submitForm('formLabelAlign')">注册</el-button>
                            </el-form-item>
                            <el-form-item>
                                <el-link type="primary" @click="login">已有账号，去登录</el-link>
                            </el-form-item>

                        </el-form>

                    </div>
                </el-col>
            </el-row>
        </el-main>
    </el-container>
</div>
</body>
<script>
    new Vue({
        el: "#app",
        data() {
            return {
                load:false,
                formLabelAlign: {
                    phone: '',
                    password: ''
                },
                rules: {
                    phone: [
                        {required: true, message: '请输入11位手机号', trigger: 'blur'},
                        {type: 'string', len: 11, message: '请输入11位手机号', trigger: 'blur'},
                    ],
                    password: [
                        {required: true, message: '请输入密码', trigger: 'blur'},
                        {type:'string',min:8,message: '最少8位',trigger: 'blur'},
                        {type:'string',max:16,message: '最多16位',trigger: 'blur'},
                    ]
                }
            }
        },
        methods: {
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        // alert('submit!');
                        this.register()
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
            register(){
                this.load=true
                console.log(this.formLabelAlign)
                this.form={
                    phone:''
                }
                this.form.phone = this.formLabelAlign.phone
                axios({
                    method:"post",
                    url:"http://localhost/user/1/1",
                    data:this.form
                }).then((resp)=>{
                    console.log(resp.data)
                    if(resp.data.totalCount==1){
                        this.$message({
                            message: "账号已注册",
                            type: "fail"
                        })
                        this.load=false
                    }else{
                        this.toRegister()
                    }
                })
            },
            toRegister(){
                axios({
                    method: "post",
                    url: "http://localhost/user",
                    data: this.formLabelAlign
                }).then((resp) => {
                    if (resp.data == "success") {
                        this.$message({
                            message: "注册成功",
                            type: "success"
                        })
                        this.load=false
                        window.location.href="http://localhost/pages/login.html"

                    }else{
                        this.$message({
                            message: "注册失败",
                            type: "fail"
                        })
                    }
                })


            },
            login(){
                window.location.href="http://localhost/pages/login.html"
            }
        }
    })
</script>
</html>